package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/1 10:19
 * @Version 1.0
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;
    //  查询所有列表
    @GetMapping("seckill.html")
    public String seckillList(Model model){
        //  调用服务层方法
        Result<List<SeckillGoods>> result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }

    //  商品秒杀详情控制 seckill/'+${item.skuId}+'.html'
    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        //  调用服务层方法
        Result<SeckillGoods> result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        return "seckill/item";
    }

    //  排队页面 http://activity.gmall.com/seckill/queue.html?skuId=25&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("seckill/queue.html")
    public String seckillQueue(HttpServletRequest request){
        //  存储数据
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  返回排队页面
        return "seckill/queue";
    }

    //  去下单控制器：
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model){
        //  userAddressList detailArrayList totalNum totalAmount
        //  会在结算页面控制器中将 detailArrayList totalNum totalAmount 放入map 中。
        Result<Map> result = activityFeignClient.authTrade();
        //  判断 code==200
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else {
            model.addAttribute("message","恭喜你，没有抢到.");
            return "seckill/fail";
        }
    }
}
